package com.hui.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hui.entity.PromotionAd;
import com.hui.entity.PromotionAdVo;
import com.hui.mapper.PromotionAdMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/06/17:40
 * @Description:
 */
@Service("promotionAdService")
public class PromotionAdServiceImpl implements PromotionAdService {
    @Autowired
    private PromotionAdMapper promotionAdMapper;

    @Override
    public PageInfo<PromotionAd> getAllPromotionAd(PromotionAdVo promotionAdVo) {
        // 设置分页
        PageHelper.startPage(promotionAdVo.getCurrentPage(), promotionAdVo.getPageSize());
        // 调用dao层，查询数据
        List<PromotionAd> list = promotionAdMapper.getAllPromotionAd();
        // 将查询结果封装到 pageInfo 中，再返回出去
        PageInfo<PromotionAd> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void savePromotionAd(PromotionAd promotionAd) {
        // 补全信息
        Date date = new Date();
        promotionAd.setCreateTime(date);
        promotionAd.setUpdateTime(date);
        // 执行添加操作
        promotionAdMapper.savePromotionAd(promotionAd);
    }

    @Override
    public void updatePromotionAd(PromotionAd promotionAd) {
        // 补全信息
        promotionAd.setUpdateTime(new Date());
        // 执行修改操作
        promotionAdMapper.updatePromotionAd(promotionAd);
    }

    @Override
    public void updateStatus(int id, int status) {
        // 封装信息
        PromotionAd promotionAd = new PromotionAd();
        promotionAd.setId(id);
        promotionAd.setStatus(status);
        promotionAd.setUpdateTime(new Date());
        // 执行修改操作
        promotionAdMapper.updateStatus(promotionAd);
    }

    @Override
    public PromotionAd getPromotionAdById(int id) {
        return promotionAdMapper.getPromotionAdById(id);
    }
}
